package com.wcm.web.client;

import com.wcm.system.model.heyufushe.ArticleImageEntity;
import com.wcm.system.repository.heyufushe.ArticleAttachementRepository;
import com.wcm.system.repository.heyufushe.ArticleAttachmentEntity;
import com.wcm.system.repository.heyufushe.ArticleImgRepository;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.List;

@RestController
@EnableAutoConfiguration
public class WenjianController {
    @Resource
    ArticleAttachementRepository articleAttachementRepository;
    @Resource
    ArticleImgRepository articleImgRepository;
    @RequestMapping("wenjian/test")
    public void te2222st1(HttpServletResponse response) throws IOException {
        int i = 0;
        Page<ArticleAttachmentEntity> articleAttachmentEntities;
        Pageable pageAble;
        FileOutputStream fos = null;
        InputStream inputStream = null;
        while (true){
         pageAble = new PageRequest(i,1);
         articleAttachmentEntities = articleAttachementRepository.findAll(pageAble);
        for(ArticleAttachmentEntity articleAttachmentEntity1:articleAttachmentEntities){
        Blob bytes = articleAttachmentEntity1.getBinary();
            try {
                 inputStream = bytes.getBinaryStream();
                  File file = new File("D:\\环保部\\核安全\\fujianbiao\\"+articleAttachmentEntity1.getDisplayLabel());
                  fos = new FileOutputStream(file);

                int len=-1;
                byte[] b=new byte[1024];
                while((len=inputStream.read(b))!=-1){
                    fos.write(b,0,len);
                }

                fos.close();
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        i++;
            System.out.println(i+"#"+articleAttachmentEntities.getTotalPages());
            if(articleAttachmentEntities==null||articleAttachmentEntities.isLast()){
                break;
            }
        }


    }
    @RequestMapping("wenjian/img/test")
    public void te2222swwwwwt1(HttpServletResponse response) throws IOException {
        int i = 0;
        Page<ArticleImageEntity> articleAttachmentEntities;
        Pageable pageAble;
        FileOutputStream fos = null;
        InputStream inputStream = null;
        while (true){
            pageAble = new PageRequest(i,1);
            articleAttachmentEntities = articleImgRepository.findAll(pageAble);
            for(ArticleImageEntity articleAttachmentEntity1:articleAttachmentEntities){
                Blob bytes = articleAttachmentEntity1.getBinary();
                try {
                    inputStream = bytes.getBinaryStream();
                    File file = new File("D:\\环保部\\核安全\\fujianbiao\\iamge\\"+articleAttachmentEntity1.getNameInArticle());
                    fos = new FileOutputStream(file);

                    int len=-1;
                    byte[] b=new byte[1024];
                    while((len=inputStream.read(b))!=-1){
                        fos.write(b,0,len);
                    }

                    fos.close();
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            i++;
            System.out.println(i+"#"+articleAttachmentEntities.getTotalPages());
            if(articleAttachmentEntities==null||articleAttachmentEntities.isLast()){
                break;
            }
        }


    }
}
